/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jmodcog2.component;

import java.util.Stack;
import jmodcog2.base.Runner;

/**
 *
 * @author Mitch
 */
public class BroadcastNode implements Node {
    Runner origin;
    private Stack<Runner> targets;

    public Runner getOrigin() {
        return origin;
    }

    public boolean containsTarget(Runner r) {
        return targets.contains(r);
    }

    public boolean removeTarget(Runner r) {
        return targets.remove(r);
    }

    public boolean setOrigin(Runner origin) {
        if (targets.isEmpty() || targets.peek().inputSize() == origin.outputSize()) {
            this.origin = origin;
            return true;
        }
        return false;
    }

    public boolean addTarget(Runner target) {
        if (origin == null || origin.outputSize() == target.inputSize() && !targets.contains(target)) {
            targets.add(target);
            return true;
        }
        return false;
    }

    @Override
    public boolean send() {
        if (origin != null && !targets.empty()) {
            for (Runner target : targets) {
                target.process(origin.getOutput());
            }
        }
        return false;
    }

    @Override
    public int originConnectionSize() {
        if (origin != null) {
            return origin.outputSize();
        }
        if (!targets.empty()) {
            return targets.peek().inputSize();
        }
        return -1;
    }

    @Override
    public int targetConnectionSize() {
        if (!targets.empty()) {
            return targets.peek().inputSize();
        }
        if (origin != null) {
            return origin.outputSize();
        }
        return -1;
    }
    
}
